<?php

require_once 'common/conn.php';
$result = array();
$usercode = $_GET['usercode'];
if (!isset($usercode)) {
    $usercode = $_POST['usercode'];
}

$thinking = $_POST['thinking'];
if (!isset($thinking)) {
    $thinking = $_GET['thinking'];
}
$habit = $_POST['habit'];
if (!isset($habit)) {
    $habit = $_GET['habit'];
}
$art = $_POST['art'];
if (!isset($art)) {
    $art = $_GET['art'];
}
$language = $_POST['language'];
if (!isset($language)) {
    $language = $_GET['language'];
}
$cognition = $_POST['cognition'];
if (!isset($cognition)) {
    $cognition = $_GET['cognition'];
}

$db = new DBC();
$sql = "SELECT deviceid FROM t_badge WHERE (SELECT deviceid FROM t_device WHERE devicecode ='" . $usercode . "') = deviceid";
$data = $db->fetchData($sql);
if ($data != null && isset($data) && count($data) > 0) {
    $sql = "UPDATE t_badge SET thinking = " . $thinking . ",habit=" . $habit . ",art=" . $art . ",language=" . $language . ",cognition=" . $cognition . ",badgeTime=now() WHERE deviceid =" . $data[0]['deviceid'];
} else {
    $sql = "INSERT INTO t_badge (deviceid,thinking,habit,art,language,cognition,badgeTime,createTime) 
        VALUES((SELECT deviceid FROM t_device WHERE devicecode ='" . $usercode . "')," . $thinking . "," . $habit . "," . $art . "," . $language . "," . $cognition . ",now(),now())";
}

if ($db->execute($sql)) {
    $result['errorNo'] = 0;
} else {
    $result['errorNo'] = 1;
}

echo json_encode($result);
?>
